import json
from ws4py.client.threadedclient import WebSocketClient


class CG_Client(WebSocketClient):


    # def opened(self):     # 连接到服务器之后就会触发onopen事件，这里用于send数据
    #     req = '{"event":"subscribe", "channel":"eth_usdt.deep"}'
    #     self.send(req)

    def closed(self, code, reason=None):
        print("Closed down:", code, reason)

    def received_message(self, resp):
        # resp = json.loads(str(resp))
        print(resp)
        # resp = json.loads(str(resp))
        # data = resp['data']
        # if type(data) is dict:
        #     ask = data['asks'][0]
        #     print('Ask:', ask)
        #     bid = data['bids'][0]
        #     print('Bid:', bid)


if __name__ == '__main__':
    ws = None
    url = 'wss://219.135.214.202:9008/v2x/platform/ws/vehicle?token=Bearer%20eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjU0MjUwNzMsInVzZXJfbmFtZSI6InN1cGVyX2FkbWluIiwianRpIjoiOTI5NzQ0M2MtNzFhNi00N2NjLWIzYjgtNjNhZDA0MGJiYmQ5IiwiY2xpZW50X2lkIjoiZnJvbnRlbmQiLCJzY29wZSI6WyJhbGwiXX0.BQhF_BSL0Bo3CqrVSlA_tKn6n_ioh7nH_5lEbdqXIpg'
    try:
        ws = CG_Client(url)
        ws.connect()
        ws.run_forever()
    except KeyboardInterrupt:
        ws.close()      #报错即关闭链接